/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../impl/head.h"
#include "../threads/head.h"
#include "notify_impl.h"
#include "engine_impl.h"

namespace xos_box
{
    ////////////////////////////////////////////////////////////////////////////////////
    // 

    static engine_impl::ITEM_DATA xos_item_data;

    ////////////////////////////////////////////////////////////////////////////////////

    engine_impl::engine_impl()
    {
        init_data();
    }

    engine_impl::~engine_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int engine_impl::get_xos_item_data( ITEM_DATA *& item_ptr )
    {
        int ret = 0;
        xos_item_data.set_container( mgr::container() );
        item_ptr = &xos_item_data;
        return ret;
    }

    int engine_impl::static_user_init()
    {
        int ret = 0;
        notify_impl::init_all();
        return ret;
    }

    int engine_impl::static_user_term()
    {
        int ret = 0;
        notify_impl::term_all();
        return ret;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int engine_impl::create_notify( i_notify ** ppv )
    {
        int ret = 0;
        notify_impl * pRet = 0;
        xos_stl::init_pool_item( pRet );
        pRet->set_engine( this );
        *ppv = pRet;
        return ret;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int engine_impl::set_fast_num( int nNum )
    {
        int ret = 0;
        m_pFast->set_max_thread_num( nNum );
        return ret;
    }

    int engine_impl::set_slow_num( int nNum )
    {
        int ret = 0;
        m_pSlow->set_max_thread_num( nNum );
        return ret;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    i_threads * engine_impl::fast_threads()
    {
        i_threads * pRet = m_pFast;
        return pRet;
    }

    i_threads * engine_impl::slow_threads()
    {
        i_threads * pRet = m_pSlow;
        return pRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int engine_impl::init_data()
    {
        int ret = 0;
        m_pFast = 0;
        m_pSlow = 0;
        return ret;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int engine_impl::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            threads_impl::T * pT = 0;
            xos_stl::init_pool_item( pT );
            m_pFast = pT;
        }

        if( 0 == ret )
        {
            threads_impl::T * pT = 0;
            xos_stl::init_pool_item( pT );
            m_pSlow = pT;
        }

        return ret;
    }

    int engine_impl::term()
    {
        int ret = 0;
        xos_stl::release_interface( m_pFast );
        xos_stl::release_interface( m_pSlow );
        init_data();
        return ret;
    }

} // xos
